package com.wang.web3;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.alexd.jsonrpc.JSONRPCException;
import org.alexd.jsonrpc.JSONRPCHttpClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link NeoWalletFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link NeoWalletFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NeoWalletFragment extends Fragment implements View.OnClickListener{
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public NeoWalletFragment() {
        // Required empty public constructor
    }

    JSONRPCHttpClient client;
    Map<String, Object> map;
    List paramsList;

    TextView mtv1;

    public static NeoWalletFragment newInstance(String param1, String param2) {
        NeoWalletFragment fragment = new NeoWalletFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_neo_wallet, container, false);
        view.findViewById(R.id.mCreateBtn).setOnClickListener(this);
        mtv1 = view.findViewById(R.id.mtv1);
        init();
        return view;
    }

    private void init() {
        String url = "http://seed3.neo.org:20332";
//        String url = "http://127.0.0.1:8545";
        client = new JSONRPCHttpClient(url);
        map = new HashMap();
        paramsList = new ArrayList();
        map.put("json-rpc", "2.0");

        map.put("params", paramsList);
        map.put("id", "1");
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mCreateBtn:
                createWallet();
                break;
        }
    }

    private void createWallet() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        paramsList.add("AJJw42jvvf1ns7ED94uyVBB5Z8RVBsnHtd");
                        String s1 = client.callString("getbalance","6PYPmdG5qP4Mk99uXYTizGTPfPRi8zTQ3TKcRxPMT9jfMfUXqMLdV4EdUS");
                        Log.e("Tag",s1);
                    } catch (JSONRPCException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
    }
}
